package com.invexer.baosteel.cashflow.mvc.controller {
import com.invexer.baosteel.cashflow.mvc.constant.CashflowNotificationConst;
import com.invexer.baosteel.cashflow.mvc.model.CashflowConfigModel;
import com.invexer.baosteel.cashflow.mvc.model.CashflowModel;
import com.invexer.common.puremvc.patterns.command.InvexerSimpleCommandBase;
import com.invexer.service.baosteel.IBaoSteelService;

import flash.events.Event;

import org.puremvc.as3.multicore.interfaces.INotification;

public class CashFlowGetDataCMD extends InvexerSimpleCommandBase {

	private var _cashflowModel:CashflowModel;
	private var _body:Object;
	override public function execute(notification:INotification):void{
		super.execute(notification);

		_cashflowModel = facade.retrieveProxy(CashflowModel.NAME) as CashflowModel;

		var configModel:CashflowConfigModel = facade.retrieveProxy(CashflowConfigModel.NAME) as CashflowConfigModel;
		var service:IBaoSteelService = configModel.httpService;

		_body = notification.getBody();
		service.getCashFlowData(onSuccess, onFail);
	}

	override public function onSuccess(data:Object):void{
		super.onSuccess(data)

		for each(var item:Object in data){
			if(item.bankac == 0){
				_cashflowModel.icbc.provisionBalance = Math.ceil(item.provisionForAmount/10000);
				_cashflowModel.icbc.unpaidBalance = Math.ceil(item.toBePaidAmount/10000);
				_cashflowModel.icbc.paidBalance = Math.ceil(item.paidAmount/10000);
				_cashflowModel.icbc.notpaidBalance = Math.ceil(item.unpaidAmount/10000);
			}else if(item.bankac == 1){
				_cashflowModel.ccb.provisionBalance = Math.ceil(item.provisionForAmount/10000);
				_cashflowModel.ccb.unpaidBalance = Math.ceil(item.toBePaidAmount/10000);
				_cashflowModel.ccb.paidBalance = Math.ceil(item.paidAmount/10000);
				_cashflowModel.ccb.notpaidBalance = Math.ceil(item.unpaidAmount/10000);
			}
		}

		_cashflowModel.total.provisionBalance = _cashflowModel.icbc.provisionBalance + _cashflowModel.ccb.provisionBalance;
		_cashflowModel.total.unpaidBalance = _cashflowModel.icbc.unpaidBalance + _cashflowModel.ccb.unpaidBalance;
		_cashflowModel.total.paidBalance = _cashflowModel.icbc.paidBalance + _cashflowModel.ccb.paidBalance;
		_cashflowModel.total.notpaidBalance = _cashflowModel.icbc.notpaidBalance + _cashflowModel.ccb.notpaidBalance;

		sendNotification(CashflowNotificationConst.CashFlowUpdateData);
	}

	override public function onFail(event:Event):void{
		super.onFail(event);
	}

//	//银行类型 0 工商 ；1 建行
//private String bankac;
//	//备付金额
//private String provisionForAmount;
//	//待付金额
//private String toBePaidAmount;
//	//已付金额
//private String paidAmount;
//	//未付金额
//private String unpaidAmount;
//	0 = {Object} [1301799697]
//		bankac = "1"
//		paidAmount = "366483.12"
//		provisionForAmount = "781532"
//		toBePaidAmount = "0"
//		unpaidAmount = "152902.48"
//	1 = {Object} [1294796449]
//		bankac = "0"
//		paidAmount = "5970492.11"
//		provisionForAmount = "9865374"
//		toBePaidAmount = "0"
//		unpaidAmount = "749003.41"
}
}
